package com.base.engine;

import org.lwjgl.Sys;
import org.lwjgl.opengl.Display;

import com.base.engine.gui.MADUserInterface;

public final class MADFrameRate {

	private static long lastFrame ; 
	private static int fps ; 
	private static long lastFPS ; 
	
	public static void start(){
		getDelta();
		lastFPS = getTime(); 
	}
	
	public static void update(){
		getDelta();
		updateFPS(); 
	}
	
	private static int getDelta(){
		long time = getTime(); 
		int delta = (int) (time - lastFrame);
		lastFrame = time ; 
		return delta ; 
	}
	
	private static long getTime(){
		return (Sys.getTime() * 1000) / Sys.getTimerResolution(); 
	}
	
	private static void updateFPS(){
		if(getTime() - lastFPS > 1000){
			Display.setTitle(MADEngine.appTitle + " | Running on "+fps+" FPS"); 
			MADUserInterface.getDebuggingScreen().getInfo().set(1, "Running on " + fps + " FPS") ; 
			fps = 0 ; 
			lastFPS += 1000 ; 
		}
		fps++ ; 
	}
	
	public static long getLastFrame(){
		return lastFrame ; 
	}
	
	
}
